package cn.stylefeng.guns.modular.ry600.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import cn.stylefeng.roses.core.base.controller.BaseController;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;

import cn.stylefeng.guns.core.log.LogObjectHolder;

import org.springframework.web.bind.annotation.RequestParam;

import com.github.pagehelper.PageHelper;

import cn.stylefeng.guns.modular.system.model.Erpkind;
import cn.stylefeng.guns.modular.ry600.service.IErpkindService;

/**
 * 账套管理控制器
 *
 * @author fengshuonan
 * @Date 2018-12-12 14:15:33
 */
@Controller
@RequestMapping("/erpkind")
public class ErpkindController extends BaseController {

    private String PREFIX = "/ry600/erpkind/";

    @Autowired
    private IErpkindService erpkindService;

    /**
     * 跳转到账套管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "erpkind.html";
    }

    /**
     * 跳转到添加账套管理
     */
    @RequestMapping("/erpkind_add")
    public String erpkindAdd() {
        return PREFIX + "erpkind_add.html";
    }

    /**
     * 跳转到修改账套管理
     */
    @RequestMapping("/erpkind_update/{erpkindId}")
    public String erpkindUpdate(@PathVariable Integer erpkindId, Model model) {
        Erpkind erpkind = erpkindService.selectById(erpkindId);
        model.addAttribute("item",erpkind);
        LogObjectHolder.me().set(erpkind);
        return PREFIX + "erpkind_edit.html";
    }

    /**
     * 获取账套管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition,HttpServletRequest request) {
    	if(!StringUtils.isBlank(request.getParameter("page")) && !StringUtils.isBlank(request.getParameter("rows"))){
	    	Integer page = Integer.valueOf(request.getParameter("page"));
			Integer rows = Integer.valueOf(request.getParameter("rows"));
			PageHelper.startPage(page, rows);
    		
    	}
		Object result=erpkindService.selectList(null);
		
        return result;
    }
    
    @RequestMapping(value = "/general")
    @ResponseBody
    public Object general(String condition,HttpServletRequest request) {
    	if(!StringUtils.isBlank(request.getParameter("page")) && !StringUtils.isBlank(request.getParameter("rows"))){
	    	Integer page = Integer.valueOf(request.getParameter("page"));
			Integer rows = Integer.valueOf(request.getParameter("rows"));
			PageHelper.startPage(page, rows);
    		
    	}
    	List<Erpkind> resultErpKind=erpkindService.selectList(null);
    	List<Map<String,String>>  resultList=new ArrayList<Map<String,String>>();
    	int point=0;
    	int num=0;
    	for(int i=0;i<resultErpKind.size();i++){
    		if("1".equals(resultErpKind.get(i).getIfMain())&&point==0){
    			num=i;
    			point=1;
    		}else{
    			Map<String,String> resultMap=new HashMap<String,String>();
        		resultMap.put("id", resultErpKind.get(i).getAccountId());
        		resultMap.put("title", resultErpKind.get(i).getAccountName());
        		resultList.add(resultMap);
    		}
    		
    		
    	}
    	if(point==1){
			Map<String,String> resultMap=new HashMap<String,String>();
    		resultMap.put("id", resultErpKind.get(num).getAccountId());
    		resultMap.put("title", resultErpKind.get(num).getAccountName());
    		resultList.add(0, resultMap);
		}
    	
    	return resultList;
		
    }

    /**
     * 新增账套管理
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Erpkind erpkind) {
        erpkindService.insert(erpkind);
        return SUCCESS_TIP;
    }

    /**
     * 删除账套管理
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer erpkindId) {
        erpkindService.deleteById(erpkindId);
        return SUCCESS_TIP;
    }

    /**
     * 修改账套管理
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Erpkind erpkind) {
        erpkindService.updateById(erpkind);
        return SUCCESS_TIP;
    }

    /**
     * 账套管理详情
     */
    @RequestMapping(value = "/detail/{erpkindId}")
    @ResponseBody
    public Object detail(@PathVariable("erpkindId") Integer erpkindId) {
        return erpkindService.selectById(erpkindId);
    }
}
